package api

import (
	"context"
	"time"

	"dev.ixmxm.top/rms/codes"
	"dev.ixmxm.top/rms/model"
	"dev.ixmxm.top/rms/utils/log"
)

// @Summary 获取数据统计
// @Description
// @Accept application/json
// @Produce application/json
// @Param type query uint8 true "数据统计类型：1=故障次数、2=充电次数、3=稼动率"
// @Param time_from query int64 true "开始时间戳"
// @Param time_to query int64 true "结束时间戳"
// @Param agv_id query uint8 false "机器人ID，为空时查全部"
// @Param page_size query int64 true "每页记录数量"
// @Param page_index query int64 true "当前页码"
// @Success 0 {object} ResponseExampleStruct "返回数据格式"
// @Success 1 {object} page.PageResultExample "data数据：分页信息"
// @Success 2 {object} []model.RobotLog "list数据：机器人异常集合"
// @Success 3 {object} []model.ChargeStatistic "list数据：充电数据集合"
// @Success 4 {object} []model.Efficency "list数据：稼动率数据集合"
// @Tags 数据统计
// @Router /statistics/get_statistics [get]
func (hg *HttpGateway) GetStatistic(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	p := params.(*getStatisticParams)
	var from, end time.Time
	if p.TimeFrom != 0 {
		from = time.Unix(p.TimeFrom, 0)
	}
	if p.TimeTo != 0 {
		end = time.Unix(p.TimeTo, 0)
	}

	var (
		statistics interface{}
		err        codes.Code
	)
	switch p.Type {
	case StatisticError:
		statistics, err = hg.dispatcher.LM().GetRobotsLogsFromDB(ctx, p.AgvID, from, end)
	case StatisticCharge:
		statistics, err = hg.dispatcher.CM().GetChargeStatistic(ctx, p.AgvID, from, end)
	case StatisticEffiency:
		statistics, err = hg.dispatcher.TM().GetEfficencyFromDB(ctx, p.AgvID, from, end)
	}
	if err != nil {
		log.ErrorWithContext(ctx, "get statistic error:", err.UnWrapError())
		return nil, err
	}
	return statistics, nil
}

// @Summary 获取数据统计
// @Description
// @Accept application/json
// @Produce application/json
// @Param time_from query int64 true "开始时间戳"
// @Param time_to query int64 true "结束时间戳"
// @Param agv_id query uint8 false "机器人ID，为空时查全部"
// @Param page_size query int64 true "每页记录数量"
// @Param page_index query int64 true "当前页码"
// @Success 0 {object} []model.Efficency "list数据：充电数据集合"
// @Tags 数据统计
// @Router /statistics/get_charger_statistics [get]
func (hg *HttpGateway) GetRechargerStatistic(ctx context.Context, params interface{}) (interface{}, codes.Code) {
	p := params.(*getChargerStatisticParams)
	var from, end time.Time
	if p.TimeFrom != 0 {
		from = time.Unix(p.TimeFrom, 0)
	}
	if p.TimeTo != 0 {
		end = time.Unix(p.TimeTo, 0)
	}

	var (
		chargerStatistics []model.ChargeStatistic
		err               codes.Code
	)
	chargerStatistics, err = hg.dispatcher.CM().GetChargeStatistic(ctx, p.AgvID, from, end)
	if err != nil {
		log.ErrorWithContext(ctx, "get statistic error:", err.UnWrapError())
		return nil, err
	}

	totalMap := make(map[string]model.Efficency, 0)
	//按照时间做分组
	for _, chargesta := range chargerStatistics {
		//获得年月日作为唯一的key
		// year := chargesta.StartTime.Year()
		// month := chargesta.StartTime.Month()
		// day := chargesta.StartTime.Day()
		//拼接成字符串
		// date := chargesta.StartTime.Format("2006-01-02")
		strKey := chargesta.AGVId
		if value, ok := totalMap[strKey]; ok {
			var efficency model.Efficency
			efficency.AGVId = chargesta.AGVId
			//只取年月日
			// efficency.StartTime = date
			efficency.TaskNum = value.TaskNum + 1
			efficency.TotalTime = chargesta.TotalTime + value.TotalTime
			totalMap[strKey] = efficency
			continue
		}
		var efficency model.Efficency
		efficency.AGVId = chargesta.AGVId
		//只取年月日
		// efficency.StartTime = date
		efficency.TaskNum = 1
		efficency.TotalTime = chargesta.TotalTime
		totalMap[strKey] = efficency
	}
	var chargerList []model.Efficency
	//将获得的数据
	for _, value := range totalMap {
		chargerList = append(chargerList, value)
	}
	return chargerList, nil
}
